//
//  DatePickerView.m
//  DatePicker
//
//  Created by Burn on 2018/11/12.
//  Copyright © 2018 nmgxysh. All rights reserved.
//

#import "DatePickerView.h"

#import "NSString+Base.h"
#import "PCHH.h"
#import <Masonry.h>


#define kTIMESTATUH 68 
//#define kTIMESTATUH 68 * [UIScreen mainScreen].bounds.size.height / 736

@interface DatePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

//  [NSString getCurrentMinute] >= 50 有这种判断都是为了显示 0分, 比如显示是14 : 50 , 那么预约最快时间也是 14 : 60 ,但是这样显示不合理,需要15 : 00 显示合理

/**时间选择*/
@property(strong,nonatomic) UIPickerView *datePikerView;

/**选择今天*/
@property(assign,nonatomic,getter=isChooseToday) BOOL chooseToday;

/**是否选择了现在时间 也就是 row = 0*/
@property(assign,nonatomic,getter=isChooseHour) BOOL chooseHour;

/**日期字符串*/
@property(copy,nonatomic) NSString *dayString;

/**时间字符串*/
@property(copy,nonatomic) NSString *hourString;

/**分钟字符串*/
@property(copy,nonatomic) NSString *minuteString;

/**取消按钮*/
@property(strong,nonatomic) UIButton *cancelBtn;

/**显示时间*/
@property(strong,nonatomic) UILabel *showTime;

/**确认*/
@property(strong,nonatomic) UIButton *submit;

/**选择预约时间提示*/
@property(strong,nonatomic) UILabel *messageLabel;

/**起点当地时间*/
@property(strong,nonatomic) UILabel *timeLabel;

/**日期数组*/
@property(strong,nonatomic) NSMutableArray *dayArray;

/**小时数组 今天的*/
@property(strong,nonatomic) NSMutableArray *todayHourArray;

/**默认时间数组*/
@property(strong,nonatomic) NSMutableArray *defaultHourArray;

/**今天当前时间的分钟数组*/
@property(strong,nonatomic) NSMutableArray *currentHourMinArray;

/**默认分钟数组*/
@property(strong,nonatomic) NSMutableArray *defaultMinArray;

/**DayIndex*/
@property(assign,nonatomic) NSInteger dayIndex;

/**HourIndex*/
@property(assign,nonatomic) NSInteger hourIndex;

/**MinIndex*/
@property(assign,nonatomic) NSInteger minIndex;

@end

@implementation DatePickerView


- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
//        60pt
        
        self.chooseToday = YES;
        self.chooseHour = YES;
        self.backgroundColor = [UIColor whiteColor];
        
        [self createUI];

    }
    return self;
}
#pragma mark - 创建UI
- (void)createUI{
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:[UIColor colorWithRed:229 / 255.0 green:229 / 255.0 blue:299 / 255.0 alpha:1] forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    self.cancelBtn.titleLabel.font = [UIFont fontWithName:FONTNAME size:17];
    
    self.submit = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.submit setTitle:@"确认" forState:UIControlStateNormal];
    self.submit.titleLabel.font = [UIFont fontWithName:FONTNAME size:17];
    [self.submit addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
    [self.submit setTitleColor:[UIColor colorWithRed:252 / 255.0 green:145 / 255.0 blue:83 / 255.0 alpha:1] forState:UIControlStateNormal];
    
    self.showTime = [[UILabel alloc]init];
    self.showTime.font = [UIFont fontWithName:FONTNAME size:18];
    self.showTime.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:self.cancelBtn];
    [self addSubview:self.submit];
    [self addSubview:self.showTime];
    
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, kTIMESTATUH - 0.5, [UIScreen mainScreen].bounds.size.width, 0.2)];
    line.backgroundColor = [UIColor colorWithRed:229 / 255.0 green:229 / 255.0 blue:299 / 255.0 alpha:1];
    [self addSubview:line];
    
    self.messageLabel = [[UILabel alloc]init];
    self.messageLabel.text = @"选择预约时间";
    self.messageLabel.font = [UIFont fontWithName:FONTNAME size:18];
    [self addSubview:self.messageLabel];
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.text = @"(起点当地时间)";
    self.timeLabel.textColor = [UIColor orangeColor];
    self.timeLabel.font = [UIFont fontWithName:FONTNAME size:15];
    [self addSubview:self.timeLabel];
}

#pragma mark - 设置Frame
- (void)layoutSubviews{
    [super layoutSubviews];
    self.datePikerView.frame = CGRectMake(0, kTIMESTATUH, self.frame.size.width, self.frame.size.height - kTIMESTATUH);
    [self addSubview:self.datePikerView];
    
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(16);
        make.top.equalTo(self).offset(kTIMESTATUH / 2 - 18);
        
    }];
    
    [self.submit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-16);
        make.top.equalTo(self).offset(kTIMESTATUH / 2 - 18);
    }];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(@(11));
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.messageLabel.mas_centerX);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(8);
    }];
}

#pragma mark - 取消点击
- (void)cancelClick{
    if ([self.delegate respondsToSelector:@selector(cancelAndSubmit)]) {
        [self.delegate cancelAndSubmit];
    }
}

#pragma mark - 确认点击
- (void)submitClick{
    if ([self.delegate respondsToSelector:@selector(cancelAndSubmit)]) {
        
        NSString *dayString = self.dayArray[self.dayIndex];
        
        NSString *hourSreing = @"";
        if (self.dayIndex == 0) {
            hourSreing = self.todayHourArray[self.hourIndex];
        }else{
            hourSreing = self.defaultHourArray[self.hourIndex];
        }
        
        NSString *min = @"";
        if (self.dayIndex == 0 & self.hourIndex == 0) {
            min = self.currentHourMinArray[self.minIndex];
        }else{
            min = self.defaultMinArray[self.minIndex];
        }
        
        dayString = [dayString stringByAppendingString:hourSreing];
        dayString = [dayString stringByAppendingString:min];
        
        NSDictionary *dateDic = [NSDictionary dictionaryWithObject:dayString forKey:@"info"];
        [[NSNotificationCenter defaultCenter] postNotificationName:SUBMITDATE object:nil userInfo:dateDic];
        
        [self.delegate cancelAndSubmit];
    }
}


#pragma mark - 内容视图
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *dateLabel = (UILabel *)view;
    if (dateLabel == nil) {
        dateLabel = [[UILabel alloc]init];
        dateLabel.textAlignment = NSTextAlignmentCenter;
        dateLabel.font = [UIFont systemFontOfSize:14];
    }
    
    switch (component) {
        case 0:
            dateLabel.text = self.dayArray[row];
            break;
        case 1:
            if (self.isChooseToday) {
            dateLabel.text = self.todayHourArray[row];
            }else{
            dateLabel.text = self.defaultHourArray[row];
            }
            break;
        case 2:
            
            if (self.isChooseToday & self.isChooseHour) {
                dateLabel.text = self.currentHourMinArray[row];
            }else{
                dateLabel.text = self.defaultMinArray[row];
            }
            break;
            
    }
    return dateLabel;
}



//#pragma mark - 内容
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    switch (component) {
//        case 0:
//            return self.dayArray[row];
//            break;
//        case 1:
//            if (self.isChooseToday) {
//                return self.todayHourArray[row];
//            }else{
//                return self.defaultHourArray[row];
//            }
//            break;
//        case 2:
//
//            if (self.isChooseToday & self.isChooseHour) {
//                return self.currentHourMinArray[row];
//            }else{
//                return self.defaultMinArray[row];
//            }
//            break;
//
//        default:
//            return nil;
//            break;
//    }
//}

#pragma mark - 返回行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        
        return self.dayArray.count;
        
    }else if (component == 1){
        if (self.isChooseToday) {
            return self.todayHourArray.count;
        }else{
            return 24;
        }
    }else{      //分钟
        if (self.isChooseHour && self.isChooseToday) {
            return self.currentHourMinArray.count;
        }else{
            return 6;
        }
        return 10;
    }
}

#pragma mark - 行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 35;
}



#pragma mark - 被选择的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    //  上一次选择的是 不是今天 不是则刷新
    if (component == 0 && row == 0) {
        self.chooseToday = YES;
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        
    }
    //   上一次选择的是 今天 那么的刷新
    if (component == 0 && row != 0) {
        if (self.chooseToday) {
            self.chooseToday = NO;
            [pickerView reloadComponent:1];
            [pickerView reloadComponent:2];
        }
    }
    
    /******************* 小时分钟 *************************/
    if (component == 1 && row == 0) {
        if (!self.chooseHour) {
            self.chooseHour = YES;
            [pickerView reloadComponent:2];
  
            
        }
    }
    if (component == 1 && row != 0) {
        if (self.chooseHour) {
            self.chooseHour = NO;
            [pickerView reloadComponent:2];
            
        }
    }

    
    if (component == 0) {
        self.dayIndex = row;
    }
    if (component == 1) {
        self.hourIndex = row;
    }
    if (component == 2) {
        self.minIndex = row;
    }
    
}




#pragma mark - 返回指定列的宽度
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    
    if (component==0) {
        return  self.frame.size.width / 2;
    } else{
        return  self.frame.size.width / 4;
    }
}

#pragma mark - 几个分组
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}


- (void)dataPickerChanged:(UIDatePicker *)datePicker {

}


- (UIPickerView *)datePikerView{
    if (!_datePikerView) {
        _datePikerView = [[UIPickerView alloc]init];
        _datePikerView.backgroundColor = [UIColor whiteColor];
        _datePikerView.dataSource = self;
        _datePikerView.delegate = self;
    }
    return _datePikerView;
}

- (NSMutableArray *)dayArray{
    if (!_dayArray) {
        _dayArray = [NSMutableArray array];
        NSInteger count = [NSString currentYearBeLeftHowManyDay];
        for (int i = 0; i < count; i++) {
            NSDate*nowDate = [NSDate date];
            NSTimeInterval  interval =24*60*60*i; //  1 * row:天数
            NSDate*date1 = [nowDate initWithTimeIntervalSinceNow:+interval];
            NSString *dateString = i == 0 ? [NSString monthDayWeeks:date1 isToday:YES] : [NSString monthDayWeeks:date1 isToday:NO];
            [_dayArray addObject:dateString];
        }
    }
    return _dayArray;
}

- (NSMutableArray *)todayHourArray{
    if (!_todayHourArray) {
        _todayHourArray = [NSMutableArray array];
        //   先获取当前时间
        NSUInteger time = [NSString getCurrentHour];
        //   在获取当前时间 是否拥有十分钟
        NSUInteger min = [NSString getCurrentMinute];
        
        if (60 - min > 10) {
            for (int i = 0; i < 24 - time; i++) {
                [_todayHourArray addObject:[NSString stringWithFormat:@"%02ld点",time + i]];
            }
        }else{
            for (int i = 1; i < 24 - time; i++) {
                [_todayHourArray addObject:[NSString stringWithFormat:@"%02ld点",time + i]];
            }
        }
    }
    return _todayHourArray;
}

- (NSMutableArray *)defaultHourArray{
    if (!_defaultHourArray) {
        _defaultHourArray = [NSMutableArray array];
        
        for (int i = 0; i < 24; i++) {
            [_defaultHourArray addObject:[NSString stringWithFormat:@"%02d点",i]];
        }
        
    }
    return _defaultHourArray;
}

- (NSMutableArray *)currentHourMinArray{
    if (!_currentHourMinArray) {
        _currentHourMinArray = [NSMutableArray array];
        NSInteger min = [NSString getCurrentMinute];
        
        //  没有剩余10分钟   也就是 00分  10分 20分
        if (min >= 50) {
            for (int i = 0; i < 6; i++) {
                [_currentHourMinArray addObject:[NSString stringWithFormat:@"%02d分",i * 10]];
                
            }
        }else{
            NSInteger count = 6 - min / 10;
            for (int i = 1; i < count ; i++) {
                [_currentHourMinArray addObject:[NSString stringWithFormat:@"%02ld分",min + i * 10]];
                
            }
        }
        
    }
    return _currentHourMinArray;
}

- (NSMutableArray *)defaultMinArray{
    if (!_defaultMinArray) {
        _defaultMinArray = [NSMutableArray array];
        for (int i = 0; i < 6; i++) {
            [_defaultMinArray addObject:[NSString stringWithFormat:@"%02d分",i * 10]];
        }
    }
    return _defaultMinArray;
}


@end
